/*
 * DinerService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.onesalt.domain.User;

/**
 * 食客Service
 *
 * @author kiri
 * @date 2015年3月25日
 * @version 1.0
 */
@Service
@Transactional
public class DinerService {

	private final UserRepository userRepository;

	@Autowired
	public DinerService(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public UserRepository getUserRepository() {
		return userRepository;
	}
	
	public Page<User> findByKeyword(String keyword, Pageable pageable) {
		List<SearchFilter> filters = new ArrayList<SearchFilter>();
		filters.add(new SearchFilter("roles.name", Operator.EQ, "diner"));
		Specification<User> spec = DynamicSpecifications.bySearchFilter(filters, User.class);
		if (pageable != null) {
			return getUserRepository().findAll(spec, pageable);
		} else {
			return new PageImpl<User>(getUserRepository().findAll(spec));
		}
	}

}
